<?php
/**
*
* @package RCB - R/C Booking
* @version $Id: export.php 9 2009-04-10 09:32:13Z scott $
* @copyright (c) 2008 Scott Deakin
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'rcb/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

$meeting_id = request_var('meeting_id',0);

/********************************************************/
$sql = 'SELECT description
        FROM ' . RCB_MEETING_TABLE . '
        WHERE meeting_id = ' . (int) $meeting_id;
$result = $db->sql_query($sql);

$filename = $db->sql_fetchfield('description',0,$result);

/********************************************************/

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');

/********************************************************/
$sql = 'SELECT d.name, d.brca, b.class_id, p.freq, p.pt
        FROM ' . RCB_BOOKING_TABLE .' AS b
        INNER JOIN ' . RCB_DRIVER_TABLE . ' as d
        	ON b.user_id = d.user_id
		LEFT JOIN ' . RCB_PROFILE_TABLE . ' as p
			ON p.user_id = b.user_id
			AND p.class_id = b.class_id
        WHERE b.meeting_id = ' . (int) $meeting_id;

$result = $db->sql_query($sql);

echo('"'. join('","',array(
	'Name', 
	'BRCA Number', 
	'Class Number', 
	'Crystals', 
	'PT No' 
	)) . "\"\n");

while($row = $db->sql_fetchrow($result)) {
	echo('"'. join('","',array(
		$row['name'], 
		$row['brca'], 
		$row['class_id'], 
		join(",", explode(" ", $row['freq']) ),
		$row['pt']
		)) . "\"\n");
}
